package com.secsys.action;

import java.util.Date;
import java.util.List;

import com.secsys.dto.MessageDTO;
import com.secsys.model.TbBbsMessage;
import com.secsys.service.MessageService;
import com.secsys.util.Constant;
import com.secsys.util.DateFormatUtils;

public class MessageAction extends BaseAction<MessageDTO> {
	private MessageDTO messageDTO;
    private MessageService messageService;
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}

	public MessageDTO getMessageDTO() {
		return messageDTO;
	}

	public void setMessageDTO(MessageDTO messageDTO) {
		this.messageDTO = messageDTO;
	}
   
	/**
	 * 
	 * @Description 发送短消息
	 * @return
	 * @throws Exception
	 */
	public String sendMsg()throws Exception{
		 System.out.println(messageDTO.getReceiverId());
		 TbBbsMessage message=new TbBbsMessage();
		  message.setContent(messageDTO.getContent());
		  message.setReviceId(Integer.parseInt(messageDTO.getReceiverId()));
		  message.setReviceName(messageDTO.getReceiver());
		  message.setTitle(messageDTO.getTitle());
		  message.setUserId(Integer.parseInt(messageDTO.getSenderId()));
		  Date date=new Date();
		  String publishTime = DateFormatUtils.convertDateTimeToString(date);
		  message.setSendTime(DateFormatUtils.convertStringToDateTime(publishTime));
		  message.setUserStatue(Constant.ABLE);
		  message.setReviceStatue(Constant.ABLE);
		  message.setUserName(messageDTO.getSenderName());
		 messageService.addMsg(message);
		 return "historyMsgList";
	}
	
	
	/**
	 * 
	 * @Description  查看邮箱 
	 * @return
	 * @throws Exception
	 */
	public String queryInbox()throws Exception{
		System.out.println(messageDTO.getSenderId());
		 List msgList=messageService.queryMsgList(Integer.parseInt(messageDTO.getSenderId().trim()),Constant.ABLE);
		 session.put("titleURL", Constant.MESSAGE_PAGE);
		 session.put("msgTitle", Constant.MSG_PAGE_INBOX);
		 session.put("msgList", msgList);
		 return "forward";
		
	}
	
	/**
	 * 
	 * @Description  已发送邮件 
	 * @return
	 * @throws Exception
	 */
	public String receviceHistory()throws Exception{
		 List msgList=messageService.queryHistorySendMsgList(Integer.parseInt(messageDTO.getSenderId()),Constant.ABLE);
		 session.put("titleURL", Constant.MESSAGE_PAGE);
		 session.put("msgTitle", Constant.MSG_PAGE_HISTORY_BOX);
		 session.put("msgList", msgList);
		 return "history";
		
	}
	
	/**
	 * 
	 * @Description 删除记录消息 
	 * @return
	 * @throws Exception
	 */
	public String delMsg()throws Exception{
		 System.out.println( messageDTO.getSenderId()+"&&&&&"+messageDTO.getId());
		TbBbsMessage message=messageService.getTbBbsMessage(Integer.parseInt( messageDTO.getId().trim()));
		if(message!=null){
		 if(messageService.validateMsgAuthor(Integer.parseInt(messageDTO.getSenderId().trim()),Integer.parseInt( messageDTO.getId().trim()))){ //是发件人
		        message.setUserStatue(Constant.DISABLE);
	      }else if(messageService.validateMsgRecevier(Integer.parseInt(messageDTO.getSenderId().trim()),Integer.parseInt( messageDTO.getId().trim()))){//是收件人
	    	  message.setReviceStatue(Constant.DISABLE);
	      }
		 messageService.delMsg(message);
		 return "queryIndex";
		}else{
			return "error";
		}
		
    }
	
	/**
	 * 
	 * @Description  通过ID查询短消息
	 * @return
	 * @throws Exception
	 */
	public String queryMsgById()throws Exception{
		TbBbsMessage message=messageService.getTbBbsMessage(Integer.parseInt( messageDTO.getId().trim()));
		session.put("message", message);
		return "query";
	}
	
	
	@Override
	public MessageDTO getModel() {
		// TODO Auto-generated method stub
		if(messageDTO==null){
			messageDTO=new MessageDTO();
		}
		return messageDTO;
	}

}
